package com.springboot.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicInteger;

@Component
@Slf4j
@Aspect
public class LogAspect2 {

    AtomicInteger i = new AtomicInteger();

    final String POINT_CUT = "pointCut1()";

    void logs(String s){
        log.info("{} - {} ",i.getAndIncrement(),s);
    }

    @Pointcut("execution(public * com.springboot.controller..*.*(..))")
    public void pointCut1(){}

    @Before(POINT_CUT)
    public void before(){
        logs("@Before");
    }

    @After(POINT_CUT)
    public void after(){
        logs("@After");
    }

    @AfterReturning(value = POINT_CUT,returning = "keys")
    public void doAfterReturningAdvice1(JoinPoint joinPoint, Object keys){
        logs("AfterReturning");
    }

    @AfterReturning(value = POINT_CUT,returning = "keys",argNames = "keys")
    public void doAfterReturningAdvice2(String keys){
        logs("AfterReturning");
    }

    @AfterThrowing(value = POINT_CUT,throwing = "exception")
    public void doAfterThrowingAdvice(JoinPoint joinPoint,Throwable exception){
        //目标方法名：
        logs("exception");
    }

    @Around(value = POINT_CUT)
    public Object doAroundAdvice(ProceedingJoinPoint proceedingJoinPoint){
        logs("Around开始");
        try {
            Object obj = proceedingJoinPoint.proceed();
            return obj;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }finally {
            logs("Around结束");
        }
        return null;
    }



}
